﻿using SIAKAD.Helper;
using SIAKADCORE.Controllers;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SIAKAD.Mahasiswas
{
    public partial class KuisionerIsi : System.Web.UI.Page
    {
        private DataSet pertanyaan;
        private Dictionary<string, string> jawabanKuisioner;

        protected void Page_Load(object sender, EventArgs e)
        {
            CheckSession();
            if (!Page.IsPostBack || !Page.IsCallback)
            {
                if (Request.QueryString["show"] == null)
                {
                    Response.Redirect("Kuisioner.aspx", false);
                    Context.ApplicationInstance.CompleteRequest();
                }

                BuatKuisioner(Request.QueryString["show"]);
            }
        }

        private void BuatKuisioner(string idPengambilan)
        {
            bool success = KuisionerController.GetTabelPreview(idPengambilan, out this.pertanyaan);

            if (!success)
            {
                Response.Redirect("Kuisioner.aspx", false);
                Context.ApplicationInstance.CompleteRequest();
            }

            System.Diagnostics.Debug.WriteLine(this.pertanyaan.Tables[0].Rows.Count);
            this.jawabanKuisioner = new Dictionary<string, string>();

            for (int i = 0; i < this.pertanyaan.Tables[0].Rows.Count; i++)
            {
                this.CreatePertanyaan(this.pertanyaan.Tables[0].Rows[i] as DataRow, i);
                this.CreateListJawaban(this.pertanyaan.Tables[0].Rows[i] as DataRow, i);
            }
            this.CreateSubmitButton();
        }

        private void CreatePertanyaan(DataRow dr, int index)
        {
            var isiPertanyaan = new Literal
            {
                ID = "pertanyaan" + index,
                Text = String.Format("{0}.&nbsp;{1}<br/>\n", index + 1, dr[3])
            };
            this.pnlPertanyaan.Controls.Add(isiPertanyaan);
            this.jawabanKuisioner.Add(dr[0].ToString(), "JWB00");
        }

        private void CreateListJawaban(DataRow dr, int index)
        {
            for (int i = 0; i < 4; i++)
            {
                var radio = new RadioButton
                {
                    ID = String.Format("jawaban{0}_{1}", index, i),
                    CssClass = "radio",
                    GroupName = dr[0].ToString(),
                    Text = dr[4 + i].ToString(),
                };

                radio.Attributes["value"] = "JWB0" + dr[8 + i].ToString();
                radio.Attributes["style"] = "padding-left: 50px;";

                this.pnlPertanyaan.Controls.Add(radio);
            }
        }

        private void CreateSubmitButton()
        {
            var submit = new Button
            {
                ID = "btSubmitJawaban",
                Text = "Submit",
            };

            submit.Click += (s, e) =>
            {
                this.ValidateKuisioner();
            };

            this.pnlPertanyaan.Controls.Add(submit);
        }

        private void ValidateKuisioner()
        {
            foreach (var control in this.pnlPertanyaan.Controls)
            {
                if (control is RadioButton)
                {
                    if ((control as RadioButton).Checked)
                    {
                        this.jawabanKuisioner[(control as RadioButton).GroupName] = (control as RadioButton).Attributes["value"];
                    }
                }
            }

            System.Diagnostics.Debug.WriteLine(this.jawabanKuisioner.Count);

            if (CheckJawaban())
            {
                System.Diagnostics.Debug.WriteLine("kurang");
            }
            else
            {
                foreach (var item in this.jawabanKuisioner)
                {
                    KuisionerController.SetJawaban(item.Key, item.Value);
                }
                KuisionerController.UpdateStatus(Request.QueryString["show"]);
                Response.Redirect("Kuisioner.aspx", false);
                Context.ApplicationInstance.CompleteRequest();
            }
        }

        private bool CheckJawaban()
        {
            bool flag = true;
            foreach (var item in this.jawabanKuisioner)
            {
                if (item.Value == "JWB00")
                    flag = false;
            }
            return !flag;
        }

        private void CheckSession()
        {
            if (Session["siakad"] != null && Session["siakad"] is SIAKADCORE.Models.Login)
            {
                if ((Session["siakad"] as SIAKADCORE.Models.Login).UserType != SIAKADCORE.Models.LoginType.Mahasiswa)
                {
                    Response.Redirect(SiteMapHelper.Check((Session["siakad"] as SIAKADCORE.Models.Login)), false);
                    Context.ApplicationInstance.CompleteRequest();
                }
                else
                {
                    this.litName.Text = MahasiswaController.GetName((Session["siakad"] as SIAKADCORE.Models.Login).IdLogin);
                }
            }
            else
            {
                Response.Redirect("/Login.aspx");
            }
        }

        protected void lnkLogout_Click(object sender, EventArgs e)
        {
            Session.Clear();
            CheckSession();
        }
    }
}